package com.tencent.face.service.impl;

import com.tencent.face.enums.FaceApi;
import com.tencent.face.param.*;
import com.tencent.face.service.TencentApiService;
import com.tencent.face.util.TencentHttpUtil;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.*;
import com.tencentcloudapi.iai.v20200303.IaiClient;
import com.tencentcloudapi.iai.v20200303.models.*;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @description:
 * @date: 2022/5/19
 * @author: shendong
 */
public class TencentApiServiceImpl implements TencentApiService {

    @Autowired
    private TencentHttpUtil tencentHttpUtil;

    @Override
    public CreateGroupResponse createGroup(GroupParam groupParam) {
        try {
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            CreateGroupResponse resp = iaiClient.CreateGroup(groupParam.initRequest());
            return resp;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public DeleteGroupResponse deleteGroup(String groupId) {
        try {
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            DeleteGroupRequest request = new DeleteGroupRequest();
            request.setGroupId(groupId);
            DeleteGroupResponse resp = iaiClient.DeleteGroup(request);
            return resp;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public GetGroupInfoResponse getGroupInfo(String groupId) {
        try {
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            GetGroupInfoRequest groupInfoRequest  =new GetGroupInfoRequest();
            groupInfoRequest.setGroupId(groupId);
            GetGroupInfoResponse getGroupInfoResponse = iaiClient.GetGroupInfo(groupInfoRequest);
            return getGroupInfoResponse;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ModifyGroupResponse modifyGroup(ModifyGroupRequest request) {
        try{
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            ModifyGroupResponse modifyGroupResponse = iaiClient.ModifyGroup(request);
            return modifyGroupResponse;
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建人员
     * @param personParam
     * @return
     */
    @Override
    public CreatePersonResponse createPerson(PersonParam personParam) {
        try{
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            CreatePersonResponse createPersonResponse = iaiClient.CreatePerson(personParam.initRequest());
            return createPersonResponse;
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public DeletePersonResponse deletePerson(String id) {
        try{
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            DeletePersonRequest req = new DeletePersonRequest();
            req.setPersonId(id);
            DeletePersonResponse deletePersonResponse = iaiClient.DeletePerson(req);
            return deletePersonResponse;
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public DeletePersonFromGroupResponse deletePersonFromGroup(String groupId, String personId) {
        try{
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            DeletePersonFromGroupRequest  req = new DeletePersonFromGroupRequest ();
            req.setPersonId(personId);
            req.setGroupId(groupId);
            return iaiClient.DeletePersonFromGroup(req);
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public CreateFaceResponse createFace(CreateFaceParam createFaceRequest) {
        try{
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            return iaiClient.CreateFace(createFaceRequest.initRequest());
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public DeleteFaceResponse deleteFace(DeleteFaceRequest request) {
        try{
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            return iaiClient.DeleteFace(request);
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public VerifyPersonResponse verifyPerson(VerifyPersonParam param) {
        try{
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            return iaiClient.VerifyPerson(param.initRequest());
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public GetLiveCodeResponse getLiveCode() {
        try{
            FaceidClient faceidClient = tencentHttpUtil.initFaceidClient(FaceApi.FACEID);
            GetLiveCodeRequest req = new GetLiveCodeRequest();
            return faceidClient.GetLiveCode(req);
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public DetectLiveFaceResponse detectLiveFace(DetectLiveFaceRequest request) {
        try {
            IaiClient iaiClient = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            return iaiClient.DetectLiveFace(request);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public SearchFacesResponse searchFaces(SearchFacesRequest req) {
        try {
            IaiClient client = tencentHttpUtil.initIaiClient(FaceApi.IAI);
            return client.SearchFaces(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public GetActionSequenceResponse getActionSequence() {
        try{
            FaceidClient faceidClient = tencentHttpUtil.initFaceidClient(FaceApi.FACEID);
            GetActionSequenceRequest  req = new GetActionSequenceRequest ();
            return faceidClient.GetActionSequence(req);
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public LivenessCompareResponse livenessCompare(LivenessCompareParam req) {
        try{
            FaceidClient faceidClient = tencentHttpUtil.initFaceidClient(FaceApi.FACEID);
            return faceidClient.LivenessCompare(req.initRequest());
        }  catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }


//    public String replaceFaceId(String faceIds,String faceImage,String phone){
//        String[] split = faceIds.split(",");
//        List<String> list = new ArrayList<>(split.length);
//        Collections.addAll(list,split);
//        if(split.length==5){ //删除其中一张后再添加  删除第二张
//            DeleteFaceRequest deleteFaceRequest = new DeleteFaceRequest();
//            deleteFaceRequest.setPersonId(phone);
//            deleteFaceRequest.setFaceIds(new String[]{list.get(1)});
//            tencentApiService.deleteFace(deleteFaceRequest);
//            list.remove(1);
//        }
//        addFace(phone,list,faceImage);
//        return list.stream().collect(Collectors.joining(","));
//    }
//
//    private void addFace(String phone, List<String> list, String imageUrl) {
//        CreateFaceParam faceParam = new CreateFaceParam();
//        faceParam.setPersonId(phone);
//        faceParam.setImages(Arrays.asList(ImageBase64Util.getImgStr(imageUrl)));
//        try {
//            CreateFaceResponse face = tencentApiService.createFace(faceParam);
//            list.add(face.getSucFaceIds()[0]);
//        } catch (Exception e) {
//            if(TencentCloudSDKException.class.isAssignableFrom(e.getClass())){
//                TencentCloudSDKException tencentCloudSDKException = (TencentCloudSDKException) e;
//                throw new BusinessException(tencentCloudSDKException.getMessage());
//            }
//        }
//    }
//
//    public String initPerson(YearPreferentialRecordParam param,String facePath){
//        if(ObjectUtil.isEmpty(facePath)){
//            return "";
//        }
//        List<String> list =new ArrayList<>();
//        String[] split = facePath.split(",");
//        for (int i = 0; i < split.length;) {
//            if(i==0){
//                PersonParam personParam = new PersonParam();
//                personParam.setGroupId(faceConfigParam.getGroupId());
//                personParam.setPersonId(param.getApplyUserPhone());
//                personParam.setPersonName(param.getChildName());
//                personParam.setImage(ImageBase64Util.getImgStr(split[i]));
//                try {
//                    CreatePersonResponse person = tencentApiService.createPerson(personParam);
//                    list.add(person.getFaceId());
//                } catch (Exception e) {
//                    if(TencentCloudSDKException.class.isAssignableFrom(e.getClass())){
//                        TencentCloudSDKException tencentCloudSDKException = (TencentCloudSDKException) e;
//                        if(tencentCloudSDKException.getErrorCode().equals("InvalidParameterValue.GroupIdNotExist")){
//                            GroupParam groupParam = new GroupParam();
//                            groupParam.setGroupId(faceConfigParam.getGroupId());
//                            groupParam.setGroupName("VIP会员库");
//                            tencentApiService.createGroup(groupParam);
//                            continue;
//                        }
//                    }
//                }
//            }else{
//                // 新增人脸
//                addFace(param.getApplyUserPhone(), list, split[i]);
//            }
//            i++;
//        }
//        return list.stream().collect(Collectors.joining(","));
//    }

}
